package com.itheima.server.service;

import cn.hutool.core.collection.CollUtil;
import com.itheima.dubbo.api.UserInfoApi;
import com.itheima.dubbo.api.mongo.CommentApi;
import com.itheima.dubbo.api.mongo.MovementApi;
import com.itheima.server.interceptor.UserHolder;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CommentService {


    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private MovementApi movementApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    /**
     * 根据动态的id查询评论列表
     * @param page
     * @param pagesize
     * @param movementId
     * @return
     */
    public PageResult findComments(Integer page, Integer pagesize, String movementId) {
        //1.根据动态的id查找评论分页对象
       PageResult pageResult =  commentApi.findComments(page,pagesize,movementId);
        //2.获取评论集合，提取评论者的id
        List<Comment> commentList = (List<Comment>) pageResult.getItems();
        List<Long> userIds = CollUtil.getFieldValues(commentList, "userId", Long.class);
        //3. 批量查询评论者的用户信息

        //4. 遍历所有的评论，构造vo
        List<CommentVo> voList = new ArrayList<>();
        if(!CollUtil.isEmpty(userIds)) {
            Map<Long, UserInfo> userInfoMap = userInfoApi.findByIds(userIds);

            for (Comment comment : commentList) {
                UserInfo userInfo = userInfoMap.get(comment.getUserId());
                CommentVo vo = CommentVo.init(userInfo, comment);
                voList.add(vo);
            }
        }
        pageResult.setItems(voList);
        return pageResult;
    }

    /**
     * 发表评论
     * @param movementId
     * @param content
     */
    public void saveComments(String movementId, String content) {
        //1. 构建一个评论对象
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(2); //评论类型 ： 1.点赞 2 评论 3喜欢
        //comment.setCommentType(CommentType.COMMENT.getType()); //高端写法: 评论类型 ： 1.点赞 2 评论 3喜欢
        comment.setContent(content); //评论的内容
        comment.setUserId(UserHolder.getId()); //评论者的id
        comment.setCreated(System.currentTimeMillis());
        comment.setLikeCount(0);
        //2. 插入评论对象
        commentApi.save(comment);
    }

    /**
     * 点赞
     * @param movementId
     * @return
     */
    public Integer likeComment(String movementId) {
        //1. 构建一个评论对象
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(1); //评论类型 ： 1.点赞 2 评论 3喜欢
        comment.setUserId(UserHolder.getId()); //评论者的id
        comment.setCreated(System.currentTimeMillis());
        comment.setLikeCount(0);
        //2. 往redis中做一个标记(某用户对某一个动态进行了点赞)
        String key = Constants.USER_LIKE_KEY+movementId+"_"+UserHolder.getId();   //USER_LIKE_SET_movementId_2
        redisTemplate.opsForValue().set(key,"1");

        return commentApi.save(comment);
    }


    /**
     * 取消点赞
     * @param movementId
     * @return
     */
    public Integer dislikeComment(String movementId) {
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(1); //评论类型 ： 1.点赞 2 评论 3喜欢
        comment.setUserId(UserHolder.getId()); //评论者的id


        String key = Constants.USER_LIKE_KEY+movementId+"_"+UserHolder.getId();   //USER_LIKE_SET_movementId_2
        //从redis中删除点赞的标记
        redisTemplate.delete(key);

        return commentApi.delete(comment);
    }

    /**
     * 喜欢
     * @param movementId
     * @return
     */
    public Integer loveComment(String movementId) {
        //1.构建一个评论对象
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(3); //评论类型 ： 1.点赞 2 评论 3喜欢
        comment.setUserId(UserHolder.getId()); //评论者的id
        comment.setCreated(System.currentTimeMillis());
        comment.setLikeCount(0);
        //2.往redis中做一个标记(某用户对某一个动态进行了点赞)
        String key = Constants.MOVEMENT_LOVE_HASHKEY+movementId+"_"+UserHolder.getId();   //USER_LIKE_SET_movementId_2
        redisTemplate.opsForValue().set(key,"1");
        //3.插入评论对象
        return commentApi.save(comment);
    }

    /**
     * 不喜欢
     * @param movementId
     * @return
     */
    public Integer disloveComment(String movementId) {
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(3); //评论类型 ： 1.点赞 2 评论 3喜欢
        comment.setUserId(UserHolder.getId()); //评论者的id
        String key = Constants.MOVEMENT_LOVE_HASHKEY+movementId+"_"+UserHolder.getId();   //USER_LIKE_SET_movementId_2
        //2从redis中删除点赞的标记
        redisTemplate.delete(key);

        return commentApi.delete(comment);
    }
}
